package cn.demoncat.util.mq.entity;

import cn.demoncat.util.lang.LocalDateUtil;
import cn.demoncat.util.lang.StringUtil;
import lombok.Data;

import java.time.LocalDateTime;

/**
 * 延时消息
 *
 * @author 延晓磊
 * @since 2021年02月23日
 */
@Data
public class MqDelayMessage {

    /**
     * 主题
     */
    private String topic;
    /**
     * KEY：均衡partition，为空时获取msg.id
     */
    private String key;
    /**
     * 消息
     */
    private MqMessage message;
    /**
     * 发送时间（优先于延迟秒数）
     */
    private LocalDateTime sendTime;
    /**
     * 延迟秒数
     */
    private Integer delaySeconds;


    public MqDelayMessage() {
    }

    public MqDelayMessage(String topic, String key, MqMessage message, LocalDateTime sendTime) {
        this.topic = topic;
        this.message = message.delay(LocalDateUtil.toStr(sendTime));
        this.key = message.key(key);
        this.sendTime = sendTime;
    }

    public MqDelayMessage(String topic, String key, MqMessage message, Integer delaySeconds) {
        this.topic = topic;
        this.message = message.delay(delaySeconds.toString());
        this.key = message.key(key);
        this.delaySeconds = delaySeconds;
    }

    public MqDelayMessage(String topic, Long key, MqMessage message, LocalDateTime sendTime) {
        this(topic, StringUtil.toString(key), message, sendTime);
    }

    public MqDelayMessage(String topic, Long key, MqMessage message, Integer delaySeconds) {
        this(topic, StringUtil.toString(key), message, delaySeconds);
    }

}
